package cn.pconline.autoclub.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author <a href="mailto:chensy0203@163.com">chensy</a>
 */
public class FileUtils {

    private static final Logger log = LoggerFactory.getLogger(FileUtils.class);

    public static List<String> readTextFile(String path) {
        return readTextFile(new File(path), "UTF-8");
    }

    public static List<String> readTextFile(File file, String charset) {
        List<String> lines = new ArrayList<String>();
        BufferedReader isr = null;
        try {
            isr = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
            String line = isr.readLine();
            while (line != null) {
                lines.add(line.trim());
                line = isr.readLine();
            }
        } catch (IOException e) {
            log.error("readTextFile fail!", e);
        } finally {
            try {
                isr.close();
            } catch (IOException ex) {
                log.error("readTextFile close inputStream fail!", ex);
            }
        }
        return lines;
    }

    public static String readHtmlFile(File file, String charset) {
        BufferedReader isr = null;
        StringBuilder sb = new StringBuilder();
        try {
            isr = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
            String line = isr.readLine();
            while (line != null) {
                line = isr.readLine();
                sb.append(line);
            }
        } catch (IOException e) {
            log.error("readHtmlFile fail!", e);
        } finally {
            try {
                isr.close();
            } catch (IOException ex) {
                log.error("readHtmlFile close inputStream fail!", ex);
            }
        }
        return sb.toString();
    }
}
